<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$query = "select `M`.`id`,`M`.`object_id`,`M`.`object_type`,`M`.`time`,`C`.`name` as `c_name`,`B`.`name` as `b_name`,`C`.`donor` as `c_donor`,`B`.`donor` as `b_donor`
	from `content_monitor` as `M`
	left join `companies` as `C`
		on `M`.`object_type` = 'insurer' and `M`.`object_id` = `C`.`company_id`
	left join `brokers` as `B`
		on `M`.`object_type` = 'broker' and `M`.`object_id` = `B`.`broker_id`
	group by `M`.`id`
	order by `M`.`id` desc";

$monitoring = $db->getTable($query);

$query = "select `S`.`monitor_flag`,`S`.`monitor_time`,`S`.`last_company`,`C`.`name`,`Ct`.`name` as `city_name`
	from `settings` as `S`
	left join `companies` as `C`
		on `S`.`last_company` = `C`.`company_id`
	left join `city` as `Ct`
		on `C`.`city_id` = `Ct`.`city_id`";
$monitor_state = $db->getRow($query);

$query = "select `C`.`company_id`,`C`.`name`,`Ct`.`name` as `city_name`
	from `companies` as `C`,`city` as `Ct`
	where
		(`address` != '' or `phone` != '') and
		`donor` != '' and `cookie` != '-1' and
		`C`.`city_id` = `Ct`.`city_id`
	order by `C`.`company_id`";
$companies = $db->getTable($query);

require_once ADMIN.DS."/head.php";
?>
<h1>Мониторинг</h1><br />
<?php
if ($monitor_state['monitor_flag'] != 0) {
	echo "<p style='color:red'>Внимание: прошлая работа мониторинга не была завершена до конца</p><br />";
}
if ($monitor_state['last_company']) {
	echo "<p>Последняя обработанная организация: (id: ".$monitor_state['last_company'].") ".$monitor_state['name']." (".$monitor_state['city_name'].")</p><br />";
}
echo "<p>Время последней удачной работы мониторинга: ".round($monitor_state['monitor_time'],4)." сек.</p><br />";
?>

Проверить с
<select name='from' id='from'>
<?php
	foreach ($companies as $company) {
		echo "<option value='{$company['company_id']}'>(id: {$company['company_id']}) {$company['name']} ({$company['city_name']})</option>\n";
	}
?>
</select>
до
<select name='to' id='to'>
<?php
	foreach ($companies as $company) {
		echo "<option value='{$company['company_id']}'>(id: {$company['company_id']}) {$company['name']} ({$company['city_name']})</option>\n";
	}
?>
</select><br /><br />

<input id='start_monitoring' type='button' value='Запустить мониторинг' />
<?php
/*if ($monitor_state['monitor_flag'] == 0) {
	echo "<input id='start_monitoring' type='button' value='Запустить мониторинг' />";
} elseif ($monitor_state['monitor_flag'] == 1) {
	echo "<input id='start_monitoring' type='button' value='Мониторинг выполняется' disabled='disabled' />";
}*/
?>
<br /><br />
<table cellpadding='0' cellspacing='0' class='list'>
	<tr class='table_header'>
		<td width='300px'>Компания</td>
		<td width='400px'>Ошибки</td>
		<td width='200px' class='actions'>Действия</td>
	</tr><?php
		$last_monitor = 0;
		foreach ($monitoring as $monitor) {

			if ($monitor['object_type'] == "insurer") {
				$text = "Компания <span style='font-weight:bold'>".$monitor['c_name']."</span><br />";
				$text .= "Донор: <a href='".$monitor['c_donor']."'>".$monitor['c_donor']."</a><br />";
				$text .= "<a class='edit' href='/admin/companies/edit/".$monitor['object_id']."'>Редактировать</a>";

			} else {
				$text = "Брокер <span style='font-weight:bold'>".$monitor['b_name']."</span><br />";
				$text .= "Донор: <a href='".$monitor['b_donor']."'>".$monitor['b_donor']."</a><br />";
				$text .= "<a class='edit' href='/admin/brokers/edit/".$monitor['object_id']."'>Редактировать</a>";
			}
			?>
			<tr>
				<td><?php echo $text; ?></td>
				<td>
					<ul style='list-style-type: square;padding-left: 5px;'>
					<?php
						$query = "select `key`,`value` from `content_errors` where `monitor_id` = '$monitor[id]' order by `id`";
						$errors = $db->getTable($query);
						foreach ($errors as $error) {
							if ($error['key'] == 'address'){
								echo "<li>изменился адрес: ".$error['value']."</li>";
							} else {
								echo "<li>пропал телефон: ".$error['value']."</li>";
							}
						}
					?>
					</ul>
				</td>
			<td class='actions'><a href='/admin/monitor/delete/<?php echo $monitor['id']; ?>' class='delete'>Удалить</a></td></tr>
		<?php
		}
?></table>

<?php require_once ADMIN.DS."/footer.php"; ?>